#!/usr/bin/perl -w
use strict;

use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin", "$FindBin::Bin");
use db_parameters;

use DBI;


print STDERR <<"HEADLINE";
888888888888888888888888888888888888888888888888888888888888888888888

    table_chromosomes

	Description:
		Obtain the number of chromosomes and the length of their DNA
	Populates
		taxon.chromosomes

888888888888888888888888888888888888888888888888888888888888888888888
HEADLINE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Determining exon structures of coding sequence


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#_________________________________________________________________________________________

#	retrieve_chrm_sequence

#		retrieve genomic sequence

#_________________________________________________________________________________________
my %taxon_data = get_taxon_data;
my %chromosome_lengths;
for my $taxon(keys %taxon_data)
{
	my $seq_dir = $taxon_data{$taxon}[SEQ_DIR] . "/dna";
	print "\tGet chromosomes for $taxon ...\n";
	my @chromosome_files = glob("$seq_dir/chromosome.*.fa");
	my $verbose = scalar @chromosome_files < 50;
	for my $chromosome_file(@chromosome_files)
	{
		$chromosome_file =~ /chromosome\.(.+)\.fa/ or die 
			die "Error:\n\tStrange chromosome file [$chromosome_file]\n";
		my $chromosome_name = $1;
		#print "\tReading from chromosome $1\n" if $verbose;
		#{
		#	unless (open FA, $chromosome_file)
		#	{	die "Error:\n\tCould not open $chromosome_file\n";
		#	}
		#	my $ignore = <FA>;
		#	local $/ = undef;
		#	my $seq = <FA>;
		#	$chromosome_lengths{$taxon}{$1} = length($seq);
		#}
		{
			die "Error:\n\tCould not open $chromosome_file\n" unless (open FA, $chromosome_file);
			#ignore first line and save position
			my $ignore = <FA>;
			my $pos = tell FA;

			# go to penultimate letter
			seek(FA, -2, 2);

			# save position
			my $len = tell(FA) - $pos;

			# if last char is not new line, length++
			my $end_char = <FA>;
			++$len if ($end_char ne "\n");
			$chromosome_lengths{$taxon}{$1} = $len;
			#print STDERR "$chromosome_lengths{$taxon}{$1}, $pos";
		}
		
	}
}





#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

# Main logic

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#
#	Get DB parameters
#
my $dbh = connect_to_panda();
use get_psql_time;
my $start_time = get_psql_time($dbh);



#
#	taxon.chromosomes
#

print STDERR "\tSaving chromosomes to panda...\n";
$dbh->do("TRUNCATE taxon.chromosomes;");
$dbh->do("COPY taxon.chromosomes".
			"(species, name, length) FROM STDIN");
for my $taxon(keys %chromosome_lengths)
{
	for my $chrm(keys %{$chromosome_lengths{$taxon}})
	{
		$dbh->pg_putline("$taxon\t$chrm\t$chromosome_lengths{$taxon}{$chrm}\n");
	}
}
$dbh->pg_putline("\\.\n");
$dbh->pg_endcopy;
$dbh->do("VACUUM FULL taxon.chromosomes;");

print STDERR "\tCompleted\n";
log_pipeline_stage($dbh, 'NULL', 10, 'table chromosomes', $start_time);
$dbh->disconnect();
